package com.hymjweb.demo.hymj.basic_data_manage.xt_airport.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.bean.XtAirportCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.bean.XtAirportPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtAirportMapper {



    class XtAirportMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("xd.id, xd.name, xd.full_pinyin,xd.camel_char_pinyin, xd.three_code, xd.four_code, xd.name_en, xd.short_name, xd.short_name_pinyin, xd.short_name_camel_char_pinyin, xd.city_ids, xd.comment");
                FROM("xt_airport xd where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("name"));
                String shortName = DataTypeUtil.toNotNullString(map.get("shortName"));
                String cityId = DataTypeUtil.toNotNullString(map.get("cityId"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and xd.name like " + DataTypeUtil.quoteAllLikeStr(name);
                }

                if(!DataTypeUtil.isEmptyStr(shortName)){
                    sql += " and xd.short_name like " + DataTypeUtil.quoteAllLikeStr(shortName);
                }

                if(!DataTypeUtil.isEmptyStr(cityId)){
                    sql += " and find_in_set( " +cityId + ",t.city_ids)";
                }
            }
            return  sql;
        }


        public String queryAirPortByKey(String key) {

            String sql= new SQL() {{
                SELECT("xd.id, xd.name, xd.full_pinyin,xd.camel_char_pinyin, xd.three_code, xd.four_code, xd.name_en, xd.short_name, xd.short_name_pinyin, xd.short_name_camel_char_pinyin, xd.city_ids, xd.comment");
                FROM("xt_airport xd ");
            }}.toString();

            sql += "where xd.id =" + DataTypeUtil.quoteStr(key);
            sql += " or xd.name like " + DataTypeUtil.quoteAllLikeStr(key);
            sql += " or xd.short_name like "  + DataTypeUtil.quoteAllLikeStr(key);
            sql += " or lower(xd.full_pinyin) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.camel_char_pinyin) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.three_code) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.four_code) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.name_en) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.short_name_pinyin) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());
            sql += " or lower(xd.short_name_camel_char_pinyin) like "  + DataTypeUtil.quoteAllLikeStr(key.toLowerCase());

            return  sql;
        }


        public String findCacheAll(Map map) {
            return new SQL() {{
                SELECT("t.id ,t.name,t.short_name,t.city_ids ");
                FROM("xt_airport t ");
            }}.toString();
        }

        public String insert(XtAirportPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("xt_airport");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("full_pinyin","#{fullPinyin}");
                VALUES("name_en","#{nameEn}");
                VALUES("short_name","#{shortName}");
                VALUES("short_name_pinyin","#{shortNamePinyin}");
                VALUES("camel_char_pinyin","#{camelCharPinyin}");
                VALUES("three_code","#{threeCode}");
                VALUES("four_code","#{fourCode}");
                VALUES("city_ids","#{cityIds}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }
        public String update(XtAirportPojo pojo) {
            String sql= new SQL() {{
                UPDATE("xt_airport xd");
                SET("xd.name = #{name}");
                SET("xd.full_pinyin = #{fullPinyin}");
                SET("xd.name_en = #{nameEn}");
                SET("xd.short_name = #{shortName}");
                SET("xd.short_name_pinyin = #{shortNamePinyin}");
                SET("xd.camel_char_pinyin = #{camelCharPinyin}");
                SET("xd.three_code = #{threeCode}");
                SET("xd.four_code = #{fourCode}");
                SET("xd.city_ids = #{cityIds}");
                SET("xd.comment = #{comment}");
                SET("xd.update_time = #{updateTime}");
                SET("xd.update_user_id = #{updateUserId}");
                SET("xd.name = #{name}");
                WHERE("xd.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_airport");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("t.*");
                FROM("xt_airport t");
                WHERE("t.id = " + id);
            }}.toString();
        }

        public String findUK(String name,String exceptId) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_airport t");
                WHERE(" t.name = " + DataTypeUtil.quoteStr(name));
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }

    }


    @Results(id = "xtAirportMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_pinyin", property = "fullPinyin"),
            @Result(column = "camel_char_pinyin", property = "camelCharPinyin"),
            @Result(column = "three_code", property = "threeCode"),
            @Result(column = "four_code", property = "fourCode"),
            @Result(column = "name_en", property = "nameEn"),
            @Result(column = "short_name", property = "shortName"),
            @Result(column = "short_name_pinyin", property = "shortNamePinyin"),
            @Result(column = "short_name_camel_char_pinyin", property = "shortNameCamelCharPinyin"),
            @Result(column = "city_ids", property = "cityIds"),
            @Result(column = "comment", property = "comment")
    })
    @SelectProvider(type = XtAirportMapperProvider.class, method = "findAll")
    List<XtAirportPojo> findAll(Map map);


    @ResultMap("xtAirportMapId")
    @SelectProvider(type = XtAirportMapperProvider.class, method = "queryAirPortByKey")
    List<XtAirportPojo> queryAirPortByKey(String key);


    @SelectProvider(type = XtAirportMapperProvider.class, method = "findCacheAll")
    @Results(id="xtAirportCacheMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "short_name", property = "shortName"),
            @Result(column = "city_ids", property = "cityIds"),
    })
    List<XtAirportCachePojo> findCacheAll(Map map);

    @ResultMap(value = "xtAirportMapId")
    @SelectProvider(type = XtAirportMapperProvider.class, method = "findObjectById")
    XtAirportPojo findObjectById(@Param("id") String id);

    @Results(id = "findUKMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = XtAirportMapperProvider.class, method = "findUK")
    XtAirportPojo findUK(String name, String exceptId);


    @InsertProvider(type = XtAirportMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtAirportPojo obj);

    @UpdateProvider(type = XtAirportMapperProvider.class, method = "update")
    int update(XtAirportPojo obj);

    @DeleteProvider(type = XtAirportMapperProvider.class, method = "delete")
    int delete(String id);



}
